/*
 *  spa.login.js
 *  유저에 관련된 작업을 하는 모듈
 */

spa.login = (function() {
/*-----------------------Module Variable Start----------------------*/
  var configMap = {
    main_html : String()
  + '<form action="" method="post" name="form" class="form-joinin">'
    + '<div class="col-md-8 col-xs-8 col-sm-8">'
      + '<h2 class="form-joinin-heading">아이디와 비밀번호를 입력하세요.</h2>'
    + '</div>'
    + '<input type="text" class="form-control spa-login-id_input" name="u_id" placeholder="아이디" autofocus>'
    + '<input type="password" class="form-control spa-login-pw_input" name="u_pass" placeholder="비밀번호">'
    + '<label class ="checkbox">'
      + '<input type="checkbox">아이디 저장'
    + '</label>'
    + '<button class="btn btn-lg btn-primary spa-login-btn-sign_in"> 로 그 인'
      + '<span class= "glyphicon glyphicon-check">'
      + '</span>'
    + '</button>'
    + '<button class="btn btn-lg btn-primary spa-login-btn-sign_up"> 회 원 가 입'
      + '<span class= "glyphicon glyphicon-user">'
      + '</span>'
    + '</button>'
  + '</form>'
  },
  stateMap = {
    $container : null,
    login_user : null
  },
  jqueryMap = {},
  initModule, setJqueryMap, login, logout;
/*-----------------------Module Variable End------------------------*/
    
/*-----------------------Utility Method Start-----------------------*/
// /login/ start
  login = function() {
    var id = $('.spa-login-id_input').val(),
        passwd = $('.spa-login-pw_input').val(),
        request;
    
    if (!id) {
      console.log(id);
      alert('ID를 입력하세요.');
      return false;
    }
    if (!passwd) {
      console.log(passwd);
      alert('비밀번호를 입력하세요.');
      return false;
    }
    console.log(id + " " + passwd); 
    $.ajax ({
      type : "POST",
      url : "jsp/Login_S.jsp",
      data : {
        userid : id,
        userpass : passwd
      },
      dataType : "json",
      timeout: 30000,
      async: false,
      success: function( response ) {
        console.log(response);
        if ( response.result === true ) {
          alert("로그인 성공");
          spa.shell.changeAnchorPart({
            modal: "deactivated"
          });
        }
        else {
          alert(response.log);
        }
      }
    });
    /*
    request.done(function(response) {
      var check = response.json[0].result,
          log = response.json[0].log,
          name = response.json[0].name;

      if (check === "false") {
        alert(log);
      }
      else {
        alert(name + " 로그인 되었습니다.");
      }
    });
    request.fail(function(jqXHR, textStatus) {
      alert( "Request failed: " + textStatus );
    });
    */
  }
// /login/ end
/*-----------------------Utility Method End-------------------------*/

/*-----------------------DOM Method Start---------------------------*/
  // /setJqueryMap/ Start
  setJqueryMap = function() {
    var $container = stateMap.$container;
    jqueryMap = {
      $container : $container
    };
  };
  // /setJqueryMap/ End
/*-----------------------DOM Method End-----------------------------*/
    
/*-----------------------Event Handler Start------------------------*/
/*-----------------------Event Handler End--------------------------*/

/*-----------------------Public Method Start------------------------*/
  // /initModule/ Start
  initModule = function($container) {
    stateMap.$container = $container;
    $container.html(configMap.main_html);
    setJqueryMap();
    
    $('.spa-login-btn-sign_in').on("click", login);
  };
  // /initModule/ End
/*-----------------------Public Method End--------------------------*/
  // Return public function
  return {initModule : initModule};
}());
